#include "polar_ssl_sha1.h"
#include "buffer.h"


#ifndef PURG_C_INTERFACE_COMPILE
namespace purg
{
#endif

  inline
  void  sha1_stream::sha_state::add_data ( buffer &b , seed &s , size_t id )
  {
    _buffer = &b;
    _length = ( 20 + 2*sizeof ( size_t ) + sizeof ( unsigned int ) );
    _buffer->resize ( _length );

    _seed_ptr = reinterpret_cast<int *> ( & (b[20]) );
    _id = reinterpret_cast<size_t *>(_seed_ptr + 1);
    _iteration = _id+1;

    *_id = id;
    *_seed_ptr = s;
    *_iteration = 0;
  }

  inline
  void  sha1_stream::sha_state::hash ()
  {
    unsigned char t[20];
    sha1::compute ( *_buffer , _length , t );
    memcpy ( *_buffer , t , 20 );
    (*_iteration)++;
  }

  inline
  void  sha1_stream::initialize ( state &s , buffer &b , seed &start , size_t rank )
  {
    s.add_data ( b , start , rank );
    update_buffer ( s , b );
  }

  inline
  void  sha1_stream::update_buffer ( state &s , buffer & )
  {
    s.hash();
  }

#ifndef PURG_C_INTERFACE_COMPILE
}
#endif


